SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[mtVerb]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[mtVerb](
	[VerbID] [int] IDENTITY(1,1) NOT NULL,
	[Text] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_mtVerb] PRIMARY KEY CLUSTERED 
(
	[VerbID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[itRawLogLine]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[itRawLogLine](
	[LineNumber] [int] NOT NULL,
	[Time] [datetime] NOT NULL,
 CONSTRAINT [PK_RawLogLine] PRIMARY KEY CLUSTERED 
(
	[LineNumber] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[faaMeterType]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[faaMeterType](
	[MeterTypeID] [smallint] IDENTITY(1,1) NOT NULL,
	[MeterDescription] [varchar](50) NOT NULL,
 CONSTRAINT [PK_faaMeterType] PRIMARY KEY CLUSTERED 
(
	[MeterTypeID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[mtNoun]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[mtNoun](
	[NounID] [int] IDENTITY(1,1) NOT NULL,
	[Text] [nvarchar](50) NOT NULL,
	[PublicText] [nvarchar](50) NULL,
 CONSTRAINT [PK_mtNouns] PRIMARY KEY CLUSTERED 
(
	[NounID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[mtRaid]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[mtRaid](
	[RaidID] [int] IDENTITY(1,1) NOT NULL,
	[Instance] [varchar](50) NOT NULL,
	[StartTime] [datetime] NOT NULL,
	[EndTime] [datetime] NOT NULL,
 CONSTRAINT [PK_mtRaid] PRIMARY KEY CLUSTERED 
(
	[RaidID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[itHealing]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[itHealing](
	[LineNumber] [int] NOT NULL,
	[Subject] [varchar](50) NULL,
	[Target] [varchar](50) NULL,
	[Amount] [int] NOT NULL,
	[Ability] [varchar](50) NULL,
 CONSTRAINT [PK_Healing] PRIMARY KEY CLUSTERED 
(
	[LineNumber] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[mtDamage]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[mtDamage](
	[RaidID] [int] NOT NULL,
	[Time] [datetime] NOT NULL,
	[SubjectID] [int] NULL,
	[TargetID] [int] NOT NULL,
	[AbilityID] [int] NULL,
	[Amount] [int] NOT NULL
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[faaMeter4d]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[faaMeter4d](
	[MeterTypeID] [smallint] NOT NULL,
	[FightID] [int] NOT NULL,
	[SubjectID] [int] NOT NULL,
	[TargetID] [int] NOT NULL,
	[VerbID] [int] NOT NULL,
	[Value] [numeric](9, 2) NOT NULL,
 CONSTRAINT [PK_faaMeter4d] PRIMARY KEY CLUSTERED 
(
	[MeterTypeID] ASC,
	[FightID] ASC,
	[SubjectID] ASC,
	[TargetID] ASC,
	[VerbID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[mtHealing]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[mtHealing](
	[RaidID] [int] NOT NULL,
	[Time] [datetime] NOT NULL,
	[SubjectID] [int] NULL,
	[TargetID] [int] NOT NULL,
	[Amount] [int] NOT NULL,
	[AbilityID] [int] NOT NULL
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[itRaidInfo]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[itRaidInfo](
	[LineNumber] [int] NOT NULL,
	[Subject] [varchar](50) NULL,
	[Zone] [varchar](50) NULL,
	[GroupNumber] [int] NULL,
	[In] [bit] NULL,
 CONSTRAINT [PK_RaidInfo] PRIMARY KEY CLUSTERED 
(
	[LineNumber] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[itChat]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[itChat](
	[LineNumber] [int] NOT NULL,
	[Text] [varchar](500) NOT NULL,
 CONSTRAINT [PK_itChat] PRIMARY KEY CLUSTERED 
(
	[LineNumber] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[itDeath]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[itDeath](
	[LineNumber] [int] NOT NULL,
	[Subject] [varchar](50) NULL,
 CONSTRAINT [PK_itDeath] PRIMARY KEY CLUSTERED 
(
	[LineNumber] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[itGuildInfo]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[itGuildInfo](
	[LineNumber] [int] NOT NULL,
	[Subject] [varchar](50) NOT NULL,
	[Zone] [varchar](50) NULL,
	[Online] [bit] NULL,
	[Note] [text] NULL,
	[OfficerNote] [text] NULL,
 CONSTRAINT [PK_GuildInfo] PRIMARY KEY CLUSTERED 
(
	[LineNumber] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[itRoll]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[itRoll](
	[LineNumber] [int] NOT NULL,
	[Subject] [varchar](50) NULL,
	[Roll] [int] NOT NULL,
	[Max] [int] NOT NULL,
	[Min] [int] NOT NULL,
 CONSTRAINT [PK_itRoll] PRIMARY KEY CLUSTERED 
(
	[LineNumber] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[itLoot]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[itLoot](
	[LineNumber] [int] NOT NULL,
	[Subject] [varchar](50) NULL,
	[Loot] [varchar](50) NOT NULL,
 CONSTRAINT [PK_LootReceived] PRIMARY KEY CLUSTERED 
(
	[LineNumber] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[itDamage]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[itDamage](
	[LineNumber] [int] NOT NULL,
	[Subject] [varchar](50) NULL,
	[Target] [varchar](50) NULL,
	[Amount] [int] NOT NULL,
	[Ability] [varchar](50) NULL,
 CONSTRAINT [PK_Damage] PRIMARY KEY CLUSTERED 
(
	[LineNumber] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[faaMeter2d]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[faaMeter2d](
	[MeterTypeID] [smallint] NOT NULL,
	[FightID] [int] NOT NULL,
	[SubjectID] [int] NOT NULL,
	[Value] [numeric](9, 2) NOT NULL,
 CONSTRAINT [PK_faaMeter2d_1] PRIMARY KEY CLUSTERED 
(
	[MeterTypeID] ASC,
	[FightID] ASC,
	[SubjectID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[mtCombatTime]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[mtCombatTime](
	[RaidID] [int] NOT NULL,
	[SubjectID] [int] NOT NULL,
	[From] [datetime] NOT NULL,
	[To] [datetime] NOT NULL,
 CONSTRAINT [PK_mtCombatTime] PRIMARY KEY CLUSTERED 
(
	[RaidID] ASC,
	[SubjectID] ASC,
	[From] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[mtDeath]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[mtDeath](
	[RaidID] [int] NOT NULL,
	[Time] [datetime] NOT NULL,
	[SubjectID] [int] NOT NULL
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[mtRaidInfo]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[mtRaidInfo](
	[RaidID] [int] NOT NULL,
	[Time] [datetime] NOT NULL,
	[SubjectID] [int] NOT NULL,
	[Zone] [varchar](50) NULL,
	[GroupNumber] [int] NULL,
	[In] [bit] NULL
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[mtLoot]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[mtLoot](
	[RaidID] [int] NOT NULL,
	[Time] [datetime] NOT NULL,
	[SubjectID] [int] NOT NULL,
	[Loot] [varchar](50) NOT NULL
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[mtRaidTime]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[mtRaidTime](
	[RaidID] [int] NOT NULL,
	[SubjectID] [int] NOT NULL,
	[From] [datetime] NOT NULL,
	[To] [datetime] NOT NULL,
 CONSTRAINT [PK_mtRaidTime] PRIMARY KEY CLUSTERED 
(
	[RaidID] ASC,
	[SubjectID] ASC,
	[From] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[mtChat]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[mtChat](
	[RaidID] [int] NOT NULL,
	[Time] [datetime] NOT NULL,
	[Text] [varchar](500) NOT NULL
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[faaFight]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[faaFight](
	[FightID] [int] IDENTITY(1,1) NOT NULL,
	[RaidID] [int] NOT NULL,
	[BeginTime] [datetime] NOT NULL,
	[EndTime] [datetime] NOT NULL,
 CONSTRAINT [PK_faaFight] PRIMARY KEY CLUSTERED 
(
	[FightID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ImportTimeDiffAnalysis]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'

-- =============================================
-- Author:		Tehnubcake
-- Create date: July 7th, 2007
-- Description:	Prints an analysis table for the time difference between import tables and raid data tables
-- =============================================
CREATE FUNCTION [dbo].[ImportTimeDiffAnalysis] 
(	
	@RaidID int
)
RETURNS TABLE 
AS
RETURN 
(
WITH TimeDiffData(RaidID, [Time], Subject, Target, Ability, Amount, TimeDiff) AS 
(
		SELECT DISTINCT 
			nmtDamage.RaidID, nmtDamage.[Time], nmtDamage.Subject, nmtDamage.Target, nmtDamage.Ability, nmtDamage.Amount,
			(convert(real,nmtDamage.[Time] - itRawLogLine.[Time])*24*60*60) TimeDiff
		FROM itRawLogLine 
			INNER JOIN itDamage ON 
				itRawLogLine.LineNumber = itDamage.LineNumber 
			INNER JOIN (SELECT     mtDamage.RaidID, mtDamage.Time, mtDamage.Amount, targetNoun.Text AS Target, subjectNoun.Text AS Subject, mtVerb.Text AS Ability
				FROM mtDamage INNER JOIN
				mtNoun AS targetNoun ON mtDamage.TargetID = targetNoun.NounID INNER JOIN
				mtNoun AS subjectNoun ON mtDamage.SubjectID = subjectNoun.NounID INNER JOIN
				mtVerb ON mtDamage.AbilityID = mtVerb.VerbID) nmtDamage ON 
				((itDamage.Ability IS NULL AND nmtDamage.Ability IS NULL) OR itDamage.Ability = nmtDamage.Ability) AND 
				itDamage.Subject = nmtDamage.Subject AND 
				itDamage.Target = nmtDamage.Target AND 
				itDamage.Amount = nmtDamage.Amount
		WHERE 
			nmtDamage.RaidID = @RaidID AND
			itDamage.Subject IS NOT NULL AND 
			itDamage.Target IS NOT NULL AND 
			itDamage.Ability IS NULL
), PercentiledData(TimeDiff,Percentile) AS (
	SELECT 
		TimeDiff, 
		NTILE(10) OVER (ORDER BY TimeDiff) Percentile
	FROM TimeDiffData 
) SELECT 
		Min(TimeDiff) [Min], 
		Max(TimeDiff) [Max], 
		Avg(TimeDiff) [Avg], 
		Sum(1) [Count], 
		Percentile
	FROM PercentiledData 
	GROUP BY percentile
)


' 
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_ClearDataTables_del]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'









-- =============================================
-- Author:		Kohist
-- Create date: 7/3/07
-- Description:	Clears all main data tables
-- =============================================
CREATE PROCEDURE [dbo].[usp_ClearDataTables_del] 
AS
BEGIN
	print''''
	print''Deleting records from mtDamage...''
	delete from mtDamage

	print''''
	print''Deleting records from mtDeath...''
	delete from mtDeath

	print''''
	print''Deleting records from mtHealing...''
	delete from mtHealing

	print''''
	print''Deleting records from mtLoot...''
	delete from mtLoot

	print''''
	print''Deleting records from mtRaid...''
	delete from mtCombatTime

	print''''
	print''Deleting records from mtChat...''
	delete from mtChat

	print''''
	print''Deleting records from mtChat...''
	delete from mtNoun

	print''''
	print''Deleting records from mtChat...''
	delete from mtVerb

	print''''
	print''Deleting records from mtRaidInfo...''
	delete from mtRaidInfo

	print''''
	print''Deleting records from mtRaidTime...''
	delete from mtRaidTime

	print''''
	print''Deleting records from mtRaid...''
	delete from mtRaid
END










' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_ClearImportTables_del]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'







-- =============================================
-- Author:		Kohist
-- Create date: 7/3/07
-- Description:	Clears all import data tables
-- =============================================
CREATE PROCEDURE [dbo].[usp_ClearImportTables_del] 
AS
BEGIN
	print''''
	print''Deleting records from itDamage...''
	delete from itDamage

	print''''
	print''Deleting records from itDeath...''
	delete from itDeath

	print''''
	print''Deleting records from itGuildInfo...''
	delete from itGuildInfo

	print''''
	print''Deleting records from itHealing...''
	delete from itHealing

	print''''
	print''Deleting records from itLoot...''
	delete from itLoot

	print''''
	print''Deleting records from itRaidInfo...''
	delete from itRaidInfo

	print''''
	print''Deleting records from itRoll...''
	delete from itRoll

	print''''
	print''Deleting records from itChat...''
	delete from itChat

	print''''
	print''Deleting records from itRawLogLine...''
	delete from itRawLogLine
END








' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_GenerateCombatWindows_ins]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'








-- =============================================
-- Author:		Kohist
-- Create date: 7/3/07
-- Description:	Commits import data into the main data tables
-- =============================================
CREATE PROCEDURE [dbo].[usp_GenerateCombatWindows_ins] 
	@iRaidID int
AS
BEGIN
	BEGIN TRANSACTION

	DECLARE @startTime datetime
	DECLARE @endTime datetime

	SELECT TOP 1 @startTime = StartTime, @endTime = EndTime from mtRaid where RaidID = @iRaidID

	PRINT ''Start Time: '' + convert(varchar, @startTime)
	PRINT ''End Time: '' + convert(varchar, @endTime)

	print ''''
	print ''Clearing mtCombatTime...''
	delete from mtCombatTime where RaidID = @iRaidID;

	print ''''
	print ''Inserting into mtCombatTime...'';
	WITH CombatEvent(SubjectID, [Time]) AS (
			SELECT SubjectID, [Time] FROM mtDamage WHERE RaidID = @iRaidID
		UNION ALL
			SELECT SubjectID, [Time] FROM mtHealing WHERE RaidID = @iRaidID
	), CombatSpan(SubjectID, [Time], [NTime]) AS (
		SELECT t.SubjectID, t.[Time], Min(n.[Time]) [NTime]
		FROM CombatEvent t
		INNER JOIN CombatEvent n ON n.SubjectID = t.SubjectID 
		WHERE 
			t.[Time] < n.[Time] and t.[Time] + (30.0/(24*60*60)) > n.[Time]
		GROUP BY t.SubjectID, t.[Time]
	), BeginTime(SubjectID, [Time], RowNumber) AS (
		SELECT n.SubjectID, n.[Time] [Time], Row_Number() OVER(ORDER BY n.SubjectID, n.[NTime])
			from CombatSpan n left join CombatSpan p on p.[NTime] = n.[Time] and p.SubjectID = n.SubjectID
			where p.SubjectID is null
	), EndTime(SubjectID, [Time], RowNumber) AS (
		SELECT t.SubjectID, t.[NTime] [Time], Row_Number() OVER(ORDER BY t.SubjectID, t.[NTime])
			from CombatSpan t left outer join CombatSpan n on t.[NTime] = n.[Time] and t.SubjectID = n.SubjectID
			where n.SubjectID is null
	), BeginEndTime(SubjectID, [From], [To]) AS (
		SELECT bt.SubjectID, bt.[Time] [From], et.[Time] [To]
			FROM BeginTime bt INNER JOIN EndTime et ON bt.SubjectID = et.SubjectID and bt.RowNumber = et.RowNumber 
	)
	INSERT INTO mtCombatTime (RaidID,SubjectID, [From], [To]) 
		SELECT @iRaidID RaidID, SubjectID, [From], [To] FROM BeginEndTime


	print ''''
	print ''Clearing mtRaidTime...''
	delete from mtRaidTime where RaidID = @iRaidID;

	print ''''
	print ''Inserting into mtRaidTime...'';
	with CombatActivity(SubjectID, [From], [To], [Next]) as
	(
		select a.SubjectID, a.[From], a.[To], Min(b.[From]) Next from mtCombatTime a 
			inner join mtCombatTime b on a.SubjectID = b.SubjectID and a.RaidID = b.RaidID and a.[To] < b.[From]
		WHERE a.RaidID = @iRaidID
		group by a.SubjectID, a.[From], a.[To]
	), RecursiveAppendedTimeWindows(SubjectID, [From], [To], [Next]) as
	(
			select a.SubjectID, a.[From], a.[To], a.[Next]
				from CombatActivity a
				full outer join CombatActivity b on a.SubjectID = b.SubjectID and b.Next = a.[From]
				where (b.Next is null or b.Next - b.[To] > (30.0/(24*60)))
		union all
			select a.SubjectID, a.[From], b.[To], b.Next 
				from RecursiveAppendedTimeWindows a 
				inner join CombatActivity b on a.SubjectID = b.SubjectID and a.Next = b.[From]
				where (a.Next - a.[To]) < (10.0/(24*60))
	)
	INSERT INTO mtRaidTime (RaidID,SubjectID, [From], [To]) 
	select @iRaidID, SubjectID, [From], max([To]) 
		from RecursiveAppendedTimeWindows
		where SubjectID is not null
		group by SubjectID, [From];

	WITH MinimumWindow(SubjectID, [Time]) AS
	(
		SELECT SubjectID, Min([From]) [Time] FROM mtRaidTime where RaidID = @iRaidID group by SubjectID
	)
	UPDATE mtRaidTime SET [From] = @startTime FROM mtRaidTime 
		inner join MinimumWindow on MinimumWindow.SubjectID = mtRaidTime.SubjectID 
		where mtRaidTime.[From] = MinimumWindow.[Time];

	WITH MaximumWindow(SubjectID, [Time]) AS
	(
		SELECT SubjectID, Max([To]) [Time] FROM mtRaidTime where RaidID = @iRaidID group by SubjectID
	)
	UPDATE mtRaidTime SET [To] = @endTime FROM mtRaidTime 
		inner join MaximumWindow on MaximumWindow.SubjectID = mtRaidTime.SubjectID 
		where MaximumWindow.[Time] = mtRaidTime.[To];

	update mtRaidTime SET [From] = mtRaidInfo.[Time]
		from mtRaidTime 
		inner join mtRaidInfo on 
			mtRaidTime.SubjectID = mtRaidInfo.SubjectID and 
			mtRaidInfo.[Time] > mtRaidTime.[From] and 
			mtRaidInfo.[Time] < mtRaidTime.[To]
		where [In] = 1

	update mtRaidTime SET [To] = mtRaidInfo.[Time]
		from mtRaidTime 
		inner join mtRaidInfo on 
			mtRaidTime.SubjectID = mtRaidInfo.SubjectID and 
			mtRaidInfo.[Time] > mtRaidTime.[From] and 
			mtRaidInfo.[Time] < mtRaidTime.[To]
		where [In] = 0

	COMMIT TRANSACTION
END









' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_GenerateMeterAnalysis_ins]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
-- =============================================
-- Author:		Andrew Charneski
-- Create date: 8/3/07
-- Description:	
-- =============================================
CREATE PROCEDURE [dbo].[usp_GenerateMeterAnalysis_ins] 
	@raidID int = 0
AS
BEGIN
	--declare @raidID int;select @raidID = 1;

	delete from faaMeter2d from faaMeter2d inner join faaFight on faaMeter2d.FightID = faaFight.FightID where faaFight.RaidID = @raidID;
	delete from faaMeter4d from faaMeter4d inner join faaFight on faaMeter4d.FightID = faaFight.FightID where faaFight.RaidID = @raidID;
	delete from faaFight where faaFight.RaidID = @raidID;

	with IndexedFights(RaidID, SubjectID, [From], [To], Number) as 
	( 
		select A.RaidID, A.SubjectID, A.[From], A.[To], ROW_NUMBER() OVER (PARTITION BY A.RaidID ORDER BY A.[From], A.[To]) Number 
			from mtCombatTime A
				left outer join mtCombatTime B on B.[From] < A.[From] and B.[To] > A.[To] and B.RaidID = A.RaidID
			where B.RaidID is null
	), IndexedEndpoints(RaidID, [From], [To], Number) as 
	(
		select IsNull(A.RaidID, B.RaidID) RaidID, A.[To], B.[From], ROW_NUMBER() OVER (PARTITION BY IsNull(A.RaidID, B.RaidID) ORDER BY A.[To], B.[From]) Number
			from IndexedFights A
			full outer join IndexedFights B on A.Number + 1 = B.Number and A.RaidID = B.RaidID
			where A.[To] is null or B.[From] is null or A.[To] < B.[From]
	)
	INSERT INTO faaFight (RaidID, BeginTime, EndTime)
	select A.RaidID, A.[To] BeginTime, B.[From] EndTime
		from IndexedEndpoints A
		inner join IndexedEndpoints B on A.Number + 1 = B.Number and A.RaidID = B.RaidID
	where A.RaidID = @raidID;


	-- Time In Combat
	insert into faaMeter2d (MeterTypeID, FightID, SubjectID, [Value])
	select 3 /*Time In Combat*/ MeterTypeID, faaFight.FightID, mtCombatTime.SubjectID,   Sum(convert(real,mtCombatTime.[To]-mtCombatTime.[From])*24*60*60) [Value] 
	from faaFight
	inner join mtCombatTime on faaFight.BeginTime <= mtCombatTime.[From] and faaFight.EndTime >= mtCombatTime.[To] and faaFight.RaidID = mtCombatTime.RaidID
	where faaFight.RaidID = @raidID
	group by faaFight.FightID, mtCombatTime.SubjectID;

	-- Damage
	insert into faaMeter4d (MeterTypeID, FightID, SubjectID, TargetID, VerbID, [Value])
	select 1 /*Damage*/ MeterTypeID, faaFight.FightID, mtDamage.SubjectID, mtDamage.TargetID,  IsNull(mtDamage.AbilityID, 301), Sum(Amount) [Value] 
	from mtDamage
	inner join faaFight on faaFight.BeginTime <= mtDamage.[Time] and faaFight.EndTime >= mtDamage.[Time] and faaFight.RaidID = mtDamage.RaidID
	where faaFight.RaidID = @raidID
	group by faaFight.FightID, mtDamage.SubjectID, mtDamage.TargetID, mtDamage.AbilityID;

	-- Healing
	insert into faaMeter4d (MeterTypeID, FightID, SubjectID, TargetID, VerbID, [Value])
	select 2 /*Healing*/ MeterTypeID, faaFight.FightID, mtHealing.SubjectID, mtHealing.TargetID,  mtHealing.AbilityID, Sum(Amount) [Value] 
	from mtHealing
	inner join faaFight on faaFight.BeginTime <= mtHealing.[Time] and faaFight.EndTime >= mtHealing.[Time] and faaFight.RaidID = mtHealing.RaidID
	where faaFight.RaidID = @raidID
	group by faaFight.FightID, mtHealing.SubjectID, mtHealing.TargetID, mtHealing.AbilityID;

END

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[vFightInfo]'))
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [dbo].[vFightInfo]
AS
with DeathLog(FightID, SubjectID, DeathNumber) as (
	select FightID, SubjectID, ROW_NUMBER() OVER (PARTITION BY FightID ORDER BY [Time]) DeathNumber 
	from mtDeath 
		inner join faaFight on faaFight.BeginTime <= mtDeath.[Time] and faaFight.EndTime >= mtDeath.[Time] and faaFight.RaidID = mtDeath.RaidID
), LastDeathIndex(FightID, DeathNumber) as (
	select FightID, Max(DeathNumber) from DeathLog group by FightID
), DamageList(RaidID, FightID, BeginTime, EndTime, DamageOut, Sequence) as (
	select faaFight.RaidID, faaFight.FightID, faaFight.BeginTime, faaFight.EndTime, Sum([Value]) DamageOut, ROW_NUMBER() OVER (PARTITION BY faaFight.RaidID ORDER BY faaFight.BeginTime) Sequence
	from faaFight
		inner join faaMeter4d on faaFight.FightID = faaMeter4d.FightID
	where MeterTypeID = 1
	group by faaFight.RaidID, faaFight.FightID, faaFight.BeginTime, faaFight.EndTime
), XDamageList(RaidID, FightID, BeginTime, EndTime, Duration, Recovery, DamageOut) as (
	select A.RaidID, A.FightID, A.BeginTime, A.EndTime,A.EndTime - A.BeginTime,B.BeginTime - A.EndTime, A.DamageOut
	from DamageList A full outer join DamageList B on B.Sequence = A.Sequence + 1 and A.RaidID = B.RaidID
)
	select XDamageList.FightID, XDamageList.BeginTime, XDamageList.EndTime, XDamageList.Duration, XDamageList.Recovery, XDamageList.DamageOut, LastDeath.PublicText LastDeath
	from XDamageList
		inner join LastDeathIndex on XDamageList.FightID = LastDeathIndex.FightID
		inner join DeathLog on XDamageList.FightID = DeathLog.FightID and DeathLog.DeathNumber = LastDeathIndex.DeathNumber
		inner join mtNoun LastDeath on LastDeath.NounID = DeathLog.SubjectID
	

' 
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[vFightMeters]'))
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [dbo].[vFightMeters]
AS
with TimeInCombat(FightID, SubjectID, TimeInCombat) as (
	select FightID, SubjectID, Sum([Value]) TimeInCombat 
	from faaMeter2d
	where MeterTypeID = 3
	group by FightID, SubjectID
), DamageOut(FightID, SubjectID, DamageOut) as (
	select FightID, SubjectID, Sum([Value]) DamageOut 
	from faaMeter4d
	where MeterTypeID = 1
	group by FightID, SubjectID
), DamageIn(FightID, SubjectID, DamageIn) as (
	select FightID, TargetID as SubjectID, Sum([Value]) DamageIn 
	from faaMeter4d
	where MeterTypeID = 1
	group by FightID, TargetID
), HealingOut(FightID, SubjectID, HealingOut) as (
	select FightID, SubjectID, Sum([Value]) HealingOut 
	from faaMeter4d
	where MeterTypeID = 2
	group by FightID, SubjectID
), HealingIn(FightID, SubjectID, HealingIn) as (
	select FightID, TargetID as SubjectID, Sum([Value]) HealingIn 
	from faaMeter4d
	where MeterTypeID = 2
	group by FightID, TargetID
) select 
		TimeInCombat.FightID,
		mtNoun.NounID NounID, 
		mtNoun.PublicText Subject, 
		TimeInCombat.TimeInCombat,
		DamageOut.DamageOut,
		DamageOut.DamageOut/TimeInCombat.TimeInCombat DPS,
		DamageIn.DamageIn,
		HealingOut.HealingOut,
		HealingOut.HealingOut/TimeInCombat.TimeInCombat HPS,
		HealingIn.HealingIn
	from mtNoun
	full outer join TimeInCombat on TimeInCombat.SubjectID = mtNoun.NounID
	full outer join DamageOut on DamageOut.SubjectID = mtNoun.NounID and DamageOut.FightID = TimeInCombat.FightID
	full outer join DamageIn on DamageIn.SubjectID = mtNoun.NounID and DamageIn.FightID = TimeInCombat.FightID
	full outer join HealingOut on HealingOut.SubjectID = mtNoun.NounID and HealingOut.FightID = TimeInCombat.FightID
	full outer join HealingIn on HealingIn.SubjectID = mtNoun.NounID and HealingIn.FightID = TimeInCombat.FightID
where mtNoun.PublicText is not null and TimeInCombat.TimeInCombat is not null' 
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LootData_LootCredit_sel]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

-- =============================================
-- Author:		Andrew Charneski
-- Create date: 8/3/07
-- Description:	
-- =============================================
CREATE PROCEDURE [dbo].[usp_LootData_LootCredit_sel] 
	-- Add the parameters for the stored procedure here
	@raidID int = 0
AS
BEGIN
	WITH Loot(RaidID, LootID, [Time]) AS 
	(
		SELECT     mtLoot.RaidID, Row_Number() OVER (PARTITION BY mtLoot.RaidID ORDER BY mtLoot.[Time], mtLoot.Loot, mtLoot.SubjectID) LootID, mtLoot.[Time]
		FROM         mtLoot 
	)
	SELECT     Loot.RaidID, Loot.LootID, mtNoun.[Text] AS Raider
	FROM         Loot 
		INNER JOIN mtRaidTime ON Loot.RaidID = mtRaidTime.RaidID AND mtRaidTime.[From] < Loot.[Time] AND mtRaidTime.[To] > Loot.[Time] 
		INNER JOIN mtNoun ON mtRaidTime.SubjectID = mtNoun.NounID
	WHERE     (Loot.RaidID = @raidID)
END


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LootData_Loot_sel]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
-- =============================================
-- Author:		Andrew Charneski
-- Create date: 8/3/07
-- Description:	
-- =============================================
CREATE PROCEDURE [dbo].[usp_LootData_Loot_sel] 
	-- Add the parameters for the stored procedure here
	@raidID int = 0
AS
BEGIN
	SELECT     mtLoot.RaidID, Row_Number() OVER (PARTITION BY mtLoot.RaidID ORDER BY mtLoot.[Time], mtLoot.Loot, mtLoot.SubjectID) LootID, mtLoot.[Time], mtLoot.Loot, mtNoun.[Text] AS Looter
	FROM         mtLoot INNER JOIN
						  mtNoun ON mtLoot.SubjectID = mtNoun.NounID
	WHERE     (mtLoot.RaidID = @raidID)
END

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LootData_LootChat_sel]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
-- =============================================
-- Author:		Andrew Charneski
-- Create date: 8/3/07
-- Description:	
-- =============================================
CREATE PROCEDURE [dbo].[usp_LootData_LootChat_sel] 
	-- Add the parameters for the stored procedure here
	@raidID int = 0
AS
BEGIN
	with LootEvents(RaidID, LootID, [Time]) as 
	(
		SELECT 
			mtLoot.RaidID, 
			Row_Number() OVER (PARTITION BY mtLoot.RaidID ORDER BY mtLoot.[Time], mtLoot.Loot, mtLoot.SubjectID) LootID, 
			mtLoot.[Time] 
		FROM mtLoot 
	), LootChat(RaidID, LootID, ChatID, [Time], [Text]) AS (
	select LootEvents.RaidID, LootEvents.LootID, Row_Number() OVER(PARTITION BY LootEvents.LootID ORDER BY mtChat.[Time] desc) ChatID, mtChat.[Time],[Text] 
	FROM LootEvents INNER JOIN mtChat on mtChat.RaidID = LootEvents.RaidID and mtChat.[Time] < LootEvents.[Time]
	) SELECT * FROM LootChat WHERE ChatID <= 10 AND RaidID = @raidID
END

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LootData_Kill_sel]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [dbo].[usp_LootData_Kill_sel]
(
	@RaidID int
)
AS
	SET NOCOUNT ON;
SELECT     mtDeath.RaidID, Row_Number() OVER (PARTITION BY mtDeath.RaidID ORDER BY mtDeath.Time, mtDeath.SubjectID) KillID, mtDeath.Time, mtNoun.Text AS [Kill]
FROM         mtDeath INNER JOIN
                      mtNoun ON mtDeath.SubjectID = mtNoun.NounID
WHERE     (mtDeath.RaidID = @RaidID)
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LootData_KillCredit_sel]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

CREATE PROCEDURE [dbo].[usp_LootData_KillCredit_sel]
(
	@RaidID int
)
AS
	SET NOCOUNT ON;
WITH Kills(RaidID,KillID,[Time]) AS 
(
	SELECT mtDeath.RaidID, Row_Number() OVER (PARTITION BY mtDeath.RaidID ORDER BY mtDeath.[Time], mtDeath.SubjectID) KillID, mtDeath.[Time]
	FROM mtDeath 
)
SELECT Kills.RaidID, Kills.KillID, mtNoun.[Text] AS Raider
FROM Kills 
	INNER JOIN mtRaidTime ON mtRaidTime.RaidID = Kills.RaidID AND mtRaidTime.[From] < Kills.[Time] AND mtRaidTime.[To] > Kills.[Time] 
	INNER JOIN mtNoun ON mtRaidTime.SubjectID = mtNoun.NounID
WHERE     (Kills.RaidID = @RaidID)

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_ImportData_ins]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

















-- =============================================
-- Author:		Kohist
-- Create date: 7/3/07
-- Description:	Commits import data into the main data tables
-- =============================================
CREATE PROCEDURE [dbo].[usp_ImportData_ins] 
	@iRaidID int,
	@iPlayer varchar(50)
AS
BEGIN
	BEGIN TRANSACTION

	DECLARE @startTime datetime
	DECLARE @endTime datetime

	SELECT TOP 1 @startTime = StartTime, @endTime = EndTime from mtRaid where RaidID = @iRaidID

	PRINT ''Start Time: '' + convert(varchar, @startTime)
	PRINT ''End Time: '' + convert(varchar, @endTime)

	DECLARE @TimeAdjustment real;
	SELECT @TimeAdjustment = [Avg] FROM [dbo].[ImportTimeDiffAnalysis] (@iRaidID) WHERE Percentile = 5
	IF @TimeAdjustment < 60
		BEGIN
			print ''Time Adjustment: '' + convert(varchar, @TimeAdjustment)
			UPDATE itRawLogLine SET [Time] = [Time] + (IsNull(@TimeAdjustment,0.0)/(24*60*60));
		END
	ELSE
		BEGIN
			print ''Supressed Time Adjustment: '' + convert(varchar, @TimeAdjustment)
		END;

	WITH NewNowns([Text]) AS
	(
		SELECT @iPlayer
		UNION ALL
		SELECT Subject FROM itDamage
		UNION ALL
		SELECT Target FROM itDamage
		UNION ALL
		SELECT Subject FROM itHealing
		UNION ALL
		SELECT Target FROM itHealing
	)
	INSERT INTO mtNoun ([Text], PublicText) 
	SELECT DISTINCT [Text], [Text] FROM NewNowns
	WHERE [Text] NOT IN (SELECT [Text] FROM mtNoun) AND [Text] IS NOT NULL;

	WITH NewVerbs([Text]) AS
	(
		SELECT Ability FROM itDamage
		UNION ALL
		SELECT Ability FROM itHealing
	)
	INSERT INTO mtVerb ([Text]) 
	SELECT DISTINCT [Text] FROM NewVerbs
	WHERE [Text] NOT IN (SELECT [Text] FROM mtVerb) AND [Text] IS NOT NULL;


	print ''''
	print ''Importing itDamage...''

	INSERT INTO mtDamage 
		(RaidID, [Time], SubjectID, TargetID, AbilityID, Amount)
	SELECT     
		@iRaidID, itRawLogLine.[Time], subjectNoun.NounID, targetNoun.NounID, mtVerb.VerbID, Amount
	FROM itRawLogLine 
		INNER JOIN itDamage ON 
			itRawLogLine.LineNumber = itDamage.LineNumber 
		INNER JOIN mtNoun AS targetNoun ON IsNull(itDamage.Target,@iPlayer) = targetNoun.Text 
		INNER JOIN mtNoun AS subjectNoun ON IsNull(itDamage.Subject,@iPlayer) = subjectNoun.Text 
		INNER JOIN mtVerb ON itDamage.Ability = mtVerb.Text
	WHERE itRawLogLine.LineNumber NOT IN
	(
		SELECT DISTINCT itRawLogLine.LineNumber
		FROM itRawLogLine 
			INNER JOIN itDamage ON 
				itRawLogLine.LineNumber = itDamage.LineNumber 
			INNER JOIN (SELECT     mtDamage.RaidID, mtDamage.Time, mtDamage.Amount, targetNoun.Text AS Target, subjectNoun.Text AS Subject, mtVerb.Text AS Ability
FROM         mtDamage INNER JOIN
                      mtNoun AS targetNoun ON mtDamage.TargetID = targetNoun.NounID INNER JOIN
                      mtNoun AS subjectNoun ON mtDamage.SubjectID = subjectNoun.NounID LEFT OUTER JOIN
                      mtVerb ON mtDamage.AbilityID = mtVerb.VerbID) nmtDamage ON 
				IsNull(itDamage.Subject,@iPlayer) = nmtDamage.Subject AND 
				IsNull(itDamage.Target,@iPlayer) = nmtDamage.Target AND 
				((itDamage.Ability IS NULL AND nmtDamage.Ability IS NULL) OR itDamage.Ability = nmtDamage.Ability) AND 
				itDamage.Amount = nmtDamage.Amount
		WHERE 
			itRawLogLine.[Time] + (0.5/(24*60*60)) > nmtDamage.[Time] AND 
			itRawLogLine.[Time] - (0.5/(24*60*60)) < nmtDamage.[Time] AND 
			nmtDamage.RaidID = @iRaidID
	)

	print ''''print ''Importing itHealing...''

	INSERT INTO mtHealing 
		(RaidID, [Time], SubjectID, TargetID, AbilityID, Amount)
	SELECT     
		@iRaidID, itRawLogLine.[Time], subjectNoun.NounID, targetNoun.NounID, mtVerb.VerbID, Amount
	FROM itRawLogLine 
		INNER JOIN itHealing ON 
			itRawLogLine.LineNumber = itHealing.LineNumber 
		INNER JOIN mtNoun AS targetNoun ON IsNull(itHealing.Target,@iPlayer) = targetNoun.Text 
		INNER JOIN mtNoun AS subjectNoun ON IsNull(itHealing.Subject,@iPlayer) = subjectNoun.Text 
		INNER JOIN mtVerb ON itHealing.Ability = mtVerb.Text
	WHERE itRawLogLine.LineNumber NOT IN
	(
		SELECT DISTINCT itRawLogLine.LineNumber
		FROM itRawLogLine 
			INNER JOIN itHealing ON 
				itRawLogLine.LineNumber = itHealing.LineNumber 
			INNER JOIN 
				(SELECT     mtHealing.RaidID, mtHealing.Time, mtHealing.Amount, targetNoun.Text AS Target, subjectNoun.Text AS Subject, mtVerb.Text AS Ability
				FROM mtHealing INNER JOIN
				mtNoun AS targetNoun ON mtHealing.TargetID = targetNoun.NounID INNER JOIN
				mtNoun AS subjectNoun ON mtHealing.SubjectID = subjectNoun.NounID INNER JOIN
				mtVerb ON mtHealing.AbilityID = mtVerb.VerbID) 
				nmtHealing ON 
					IsNull(itHealing.Subject,@iPlayer) = nmtHealing.Subject AND 
					IsNull(itHealing.Target,@iPlayer) = nmtHealing.Target AND 
					itHealing.Ability = nmtHealing.Ability AND 
					itHealing.Amount = nmtHealing.Amount
		WHERE 
			itRawLogLine.[Time] + (0.5/(24*60*60)) > nmtHealing.[Time] AND 
			itRawLogLine.[Time] - (0.5/(24*60*60)) < nmtHealing.[Time] AND 
			nmtHealing.RaidID = @iRaidID
	)

	print ''''
	print ''Importing itLoot...''

	INSERT INTO mtLoot 
		(RaidID, [Time], SubjectID, Loot)
	SELECT     
		@iRaidID, itRawLogLine.[Time], mtNoun.NounID, Loot
	FROM itRawLogLine 
		INNER JOIN itLoot ON 
			itRawLogLine.LineNumber = itLoot.LineNumber 
INNER JOIN
                      mtNoun ON IsNull(itLoot.Subject,@iPlayer) = mtNoun.Text
	WHERE itRawLogLine.LineNumber NOT IN
	(
		SELECT DISTINCT itRawLogLine.LineNumber
		FROM itRawLogLine 
			INNER JOIN itLoot ON 
				itRawLogLine.LineNumber = itLoot.LineNumber 
			INNER JOIN (SELECT mtLoot.RaidID, mtLoot.Time, mtLoot.Loot, mtNoun.NounID AS Subject FROM mtLoot INNER JOIN mtNoun ON mtLoot.SubjectID = mtNoun.NounID) nmtLoot ON 
				IsNull(itLoot.Subject,@iPlayer) = nmtLoot.Subject AND 
				itLoot.Loot = nmtLoot.Loot
		WHERE 
			itRawLogLine.[Time] + (0.5/(24*60*60)) > nmtLoot.[Time] AND 
			itRawLogLine.[Time] - (0.5/(24*60*60)) < nmtLoot.[Time] AND 
			nmtLoot.RaidID = @iRaidID
	)

	print ''''
	print ''Importing itDeath...''	

	INSERT INTO mtDeath 
		(RaidID, [Time], SubjectID)
	SELECT     
		@iRaidID, itRawLogLine.[Time], mtNoun.NounID
	FROM itRawLogLine 
		INNER JOIN itDeath ON 
			itRawLogLine.LineNumber = itDeath.LineNumber  INNER JOIN
                      mtNoun ON IsNull(itDeath.Subject,@iPlayer) = mtNoun.Text
	WHERE itRawLogLine.LineNumber NOT IN
	(
		SELECT DISTINCT itRawLogLine.LineNumber
		FROM itRawLogLine 
			INNER JOIN itDeath ON 
				itRawLogLine.LineNumber = itDeath.LineNumber 
			INNER JOIN (SELECT mtDeath.RaidID, mtDeath.Time, mtNoun.[Text] AS Subject FROM mtDeath INNER JOIN mtNoun ON mtDeath.SubjectID = mtNoun.NounID) nmtDeath 
				ON IsNull(itDeath.Subject,@iPlayer) = nmtDeath.Subject
		WHERE 
			itRawLogLine.[Time] + (0.5/(24*60*60)) > nmtDeath.[Time] AND 
			itRawLogLine.[Time] - (0.5/(24*60*60)) < nmtDeath.[Time] AND 
			nmtDeath.RaidID = @iRaidID
	)

	print ''''
	print ''Importing itRaidInfo...''		

	INSERT INTO mtRaidInfo 
		(RaidID, [Time], SubjectID, Zone, GroupNumber, [In])
	SELECT     
		@iRaidID, itRawLogLine.[Time], SubjectTable.NounID AS SubjectID, Zone, GroupNumber, [In]
	FROM itRawLogLine 
		INNER JOIN itRaidInfo ON 
			itRawLogLine.LineNumber = itRaidInfo.LineNumber 
		INNER JOIN mtNoun SubjectTable ON 
			SubjectTable.[Text] = IsNull(itRaidInfo.Subject,@iPlayer)
	WHERE itRawLogLine.LineNumber NOT IN
	(
		SELECT DISTINCT itRawLogLine.LineNumber
		FROM itRawLogLine 
			INNER JOIN itRaidInfo ON 
				itRawLogLine.LineNumber = itRaidInfo.LineNumber 
			INNER JOIN (SELECT mtRaidInfo.RaidID, mtRaidInfo.Time, mtRaidInfo.Zone, mtRaidInfo.GroupNumber, mtRaidInfo.[In], mtNoun.[Text] AS Subject FROM mtRaidInfo INNER JOIN mtNoun ON mtNoun.NounID = mtRaidInfo.SubjectID) nmtRaidInfo ON 
				itRaidInfo.Subject = nmtRaidInfo.Subject and
				((itRaidInfo.Zone is null and nmtRaidInfo.Zone is null) or itRaidInfo.Zone = nmtRaidInfo.Zone) and
				((itRaidInfo.GroupNumber is null and nmtRaidInfo.GroupNumber is null) or itRaidInfo.GroupNumber = nmtRaidInfo.GroupNumber) and
				((itRaidInfo.[In] is null and nmtRaidInfo.[In] is null) or itRaidInfo.[In] = nmtRaidInfo.[In])
		WHERE 
			itRawLogLine.[Time] + (0.5/(24*60*60)) > nmtRaidInfo.[Time] AND 
			itRawLogLine.[Time] - (0.5/(24*60*60)) < nmtRaidInfo.[Time] AND 
			nmtRaidInfo.RaidID = @iRaidID
	)

	print ''''
	print ''Importing itChat...''		

	INSERT INTO mtChat 
		(RaidID, [Time], [Text])
	SELECT     
		@iRaidID, itRawLogLine.[Time], [Text]
	FROM itRawLogLine 
		INNER JOIN itChat ON 
			itRawLogLine.LineNumber = itChat.LineNumber 
	WHERE itRawLogLine.LineNumber NOT IN
	(
		SELECT DISTINCT itRawLogLine.LineNumber
		FROM itRawLogLine 
			INNER JOIN itChat ON itRawLogLine.LineNumber = itChat.LineNumber 
			INNER JOIN mtChat ON itChat.[Text] = mtChat.[Text]
		WHERE 
			itRawLogLine.[Time] + (0.5/(24*60*60)) > mtChat.[Time] AND 
			itRawLogLine.[Time] - (0.5/(24*60*60)) < mtChat.[Time] AND 
			mtChat.RaidID = @iRaidID
	)

	COMMIT TRANSACTION
END


















' 
END
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtDamage_mtNoun]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtDamage]'))
ALTER TABLE [dbo].[mtDamage]  WITH CHECK ADD  CONSTRAINT [FK_mtDamage_mtNoun] FOREIGN KEY([SubjectID])
REFERENCES [dbo].[mtNoun] ([NounID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtDamage_mtNoun1]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtDamage]'))
ALTER TABLE [dbo].[mtDamage]  WITH CHECK ADD  CONSTRAINT [FK_mtDamage_mtNoun1] FOREIGN KEY([TargetID])
REFERENCES [dbo].[mtNoun] ([NounID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtDamage_mtRaid]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtDamage]'))
ALTER TABLE [dbo].[mtDamage]  WITH CHECK ADD  CONSTRAINT [FK_mtDamage_mtRaid] FOREIGN KEY([RaidID])
REFERENCES [dbo].[mtRaid] ([RaidID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtDamage_mtVerb]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtDamage]'))
ALTER TABLE [dbo].[mtDamage]  WITH CHECK ADD  CONSTRAINT [FK_mtDamage_mtVerb] FOREIGN KEY([AbilityID])
REFERENCES [dbo].[mtVerb] ([VerbID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_faaMeter4d_faaFight]') AND parent_object_id = OBJECT_ID(N'[dbo].[faaMeter4d]'))
ALTER TABLE [dbo].[faaMeter4d]  WITH CHECK ADD  CONSTRAINT [FK_faaMeter4d_faaFight] FOREIGN KEY([FightID])
REFERENCES [dbo].[faaFight] ([FightID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_faaMeter4d_faaMeter4dType]') AND parent_object_id = OBJECT_ID(N'[dbo].[faaMeter4d]'))
ALTER TABLE [dbo].[faaMeter4d]  WITH CHECK ADD  CONSTRAINT [FK_faaMeter4d_faaMeter4dType] FOREIGN KEY([MeterTypeID])
REFERENCES [dbo].[faaMeterType] ([MeterTypeID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_faaMeter4d_mtNoun]') AND parent_object_id = OBJECT_ID(N'[dbo].[faaMeter4d]'))
ALTER TABLE [dbo].[faaMeter4d]  WITH CHECK ADD  CONSTRAINT [FK_faaMeter4d_mtNoun] FOREIGN KEY([SubjectID])
REFERENCES [dbo].[mtNoun] ([NounID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_faaMeter4d_mtNoun1]') AND parent_object_id = OBJECT_ID(N'[dbo].[faaMeter4d]'))
ALTER TABLE [dbo].[faaMeter4d]  WITH CHECK ADD  CONSTRAINT [FK_faaMeter4d_mtNoun1] FOREIGN KEY([TargetID])
REFERENCES [dbo].[mtNoun] ([NounID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_faaMeter4d_mtVerb]') AND parent_object_id = OBJECT_ID(N'[dbo].[faaMeter4d]'))
ALTER TABLE [dbo].[faaMeter4d]  WITH CHECK ADD  CONSTRAINT [FK_faaMeter4d_mtVerb] FOREIGN KEY([VerbID])
REFERENCES [dbo].[mtVerb] ([VerbID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtHealing_mtNoun]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtHealing]'))
ALTER TABLE [dbo].[mtHealing]  WITH CHECK ADD  CONSTRAINT [FK_mtHealing_mtNoun] FOREIGN KEY([SubjectID])
REFERENCES [dbo].[mtNoun] ([NounID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtHealing_mtNoun1]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtHealing]'))
ALTER TABLE [dbo].[mtHealing]  WITH CHECK ADD  CONSTRAINT [FK_mtHealing_mtNoun1] FOREIGN KEY([TargetID])
REFERENCES [dbo].[mtNoun] ([NounID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtHealing_mtRaid]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtHealing]'))
ALTER TABLE [dbo].[mtHealing]  WITH CHECK ADD  CONSTRAINT [FK_mtHealing_mtRaid] FOREIGN KEY([RaidID])
REFERENCES [dbo].[mtRaid] ([RaidID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtHealing_mtVerb]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtHealing]'))
ALTER TABLE [dbo].[mtHealing]  WITH CHECK ADD  CONSTRAINT [FK_mtHealing_mtVerb] FOREIGN KEY([AbilityID])
REFERENCES [dbo].[mtVerb] ([VerbID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_itRaidInfo_itRawLogLine]') AND parent_object_id = OBJECT_ID(N'[dbo].[itRaidInfo]'))
ALTER TABLE [dbo].[itRaidInfo]  WITH CHECK ADD  CONSTRAINT [FK_itRaidInfo_itRawLogLine] FOREIGN KEY([LineNumber])
REFERENCES [dbo].[itRawLogLine] ([LineNumber])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_itChat_itRawLogLine]') AND parent_object_id = OBJECT_ID(N'[dbo].[itChat]'))
ALTER TABLE [dbo].[itChat]  WITH CHECK ADD  CONSTRAINT [FK_itChat_itRawLogLine] FOREIGN KEY([LineNumber])
REFERENCES [dbo].[itRawLogLine] ([LineNumber])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_itDeath_itRawLogLine]') AND parent_object_id = OBJECT_ID(N'[dbo].[itDeath]'))
ALTER TABLE [dbo].[itDeath]  WITH CHECK ADD  CONSTRAINT [FK_itDeath_itRawLogLine] FOREIGN KEY([LineNumber])
REFERENCES [dbo].[itRawLogLine] ([LineNumber])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_itGuildInfo_itRawLogLine]') AND parent_object_id = OBJECT_ID(N'[dbo].[itGuildInfo]'))
ALTER TABLE [dbo].[itGuildInfo]  WITH CHECK ADD  CONSTRAINT [FK_itGuildInfo_itRawLogLine] FOREIGN KEY([LineNumber])
REFERENCES [dbo].[itRawLogLine] ([LineNumber])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_itRoll_itRawLogLine]') AND parent_object_id = OBJECT_ID(N'[dbo].[itRoll]'))
ALTER TABLE [dbo].[itRoll]  WITH CHECK ADD  CONSTRAINT [FK_itRoll_itRawLogLine] FOREIGN KEY([LineNumber])
REFERENCES [dbo].[itRawLogLine] ([LineNumber])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_itLoot_itRawLogLine]') AND parent_object_id = OBJECT_ID(N'[dbo].[itLoot]'))
ALTER TABLE [dbo].[itLoot]  WITH CHECK ADD  CONSTRAINT [FK_itLoot_itRawLogLine] FOREIGN KEY([LineNumber])
REFERENCES [dbo].[itRawLogLine] ([LineNumber])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_itDamage_itRawLogLine]') AND parent_object_id = OBJECT_ID(N'[dbo].[itDamage]'))
ALTER TABLE [dbo].[itDamage]  WITH CHECK ADD  CONSTRAINT [FK_itDamage_itRawLogLine] FOREIGN KEY([LineNumber])
REFERENCES [dbo].[itRawLogLine] ([LineNumber])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_faaMeter2d_faaFight]') AND parent_object_id = OBJECT_ID(N'[dbo].[faaMeter2d]'))
ALTER TABLE [dbo].[faaMeter2d]  WITH CHECK ADD  CONSTRAINT [FK_faaMeter2d_faaFight] FOREIGN KEY([FightID])
REFERENCES [dbo].[faaFight] ([FightID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_faaMeter2d_faaMeter2dType]') AND parent_object_id = OBJECT_ID(N'[dbo].[faaMeter2d]'))
ALTER TABLE [dbo].[faaMeter2d]  WITH CHECK ADD  CONSTRAINT [FK_faaMeter2d_faaMeter2dType] FOREIGN KEY([MeterTypeID])
REFERENCES [dbo].[faaMeterType] ([MeterTypeID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_faaMeter2d_mtNoun]') AND parent_object_id = OBJECT_ID(N'[dbo].[faaMeter2d]'))
ALTER TABLE [dbo].[faaMeter2d]  WITH CHECK ADD  CONSTRAINT [FK_faaMeter2d_mtNoun] FOREIGN KEY([SubjectID])
REFERENCES [dbo].[mtNoun] ([NounID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtCombatTime_mtNoun]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtCombatTime]'))
ALTER TABLE [dbo].[mtCombatTime]  WITH CHECK ADD  CONSTRAINT [FK_mtCombatTime_mtNoun] FOREIGN KEY([SubjectID])
REFERENCES [dbo].[mtNoun] ([NounID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtCombatTime_mtRaid]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtCombatTime]'))
ALTER TABLE [dbo].[mtCombatTime]  WITH CHECK ADD  CONSTRAINT [FK_mtCombatTime_mtRaid] FOREIGN KEY([RaidID])
REFERENCES [dbo].[mtRaid] ([RaidID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtDeath_mtNoun]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtDeath]'))
ALTER TABLE [dbo].[mtDeath]  WITH CHECK ADD  CONSTRAINT [FK_mtDeath_mtNoun] FOREIGN KEY([SubjectID])
REFERENCES [dbo].[mtNoun] ([NounID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtDeath_mtRaid]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtDeath]'))
ALTER TABLE [dbo].[mtDeath]  WITH CHECK ADD  CONSTRAINT [FK_mtDeath_mtRaid] FOREIGN KEY([RaidID])
REFERENCES [dbo].[mtRaid] ([RaidID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtRaidInfo_mtNoun]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtRaidInfo]'))
ALTER TABLE [dbo].[mtRaidInfo]  WITH CHECK ADD  CONSTRAINT [FK_mtRaidInfo_mtNoun] FOREIGN KEY([SubjectID])
REFERENCES [dbo].[mtNoun] ([NounID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtRaidInfo_mtRaid]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtRaidInfo]'))
ALTER TABLE [dbo].[mtRaidInfo]  WITH CHECK ADD  CONSTRAINT [FK_mtRaidInfo_mtRaid] FOREIGN KEY([RaidID])
REFERENCES [dbo].[mtRaid] ([RaidID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtLoot_mtNoun]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtLoot]'))
ALTER TABLE [dbo].[mtLoot]  WITH CHECK ADD  CONSTRAINT [FK_mtLoot_mtNoun] FOREIGN KEY([SubjectID])
REFERENCES [dbo].[mtNoun] ([NounID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtLoot_mtRaid]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtLoot]'))
ALTER TABLE [dbo].[mtLoot]  WITH CHECK ADD  CONSTRAINT [FK_mtLoot_mtRaid] FOREIGN KEY([RaidID])
REFERENCES [dbo].[mtRaid] ([RaidID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtRaidTime_mtNoun]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtRaidTime]'))
ALTER TABLE [dbo].[mtRaidTime]  WITH CHECK ADD  CONSTRAINT [FK_mtRaidTime_mtNoun] FOREIGN KEY([SubjectID])
REFERENCES [dbo].[mtNoun] ([NounID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtRaidTime_mtRaid]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtRaidTime]'))
ALTER TABLE [dbo].[mtRaidTime]  WITH CHECK ADD  CONSTRAINT [FK_mtRaidTime_mtRaid] FOREIGN KEY([RaidID])
REFERENCES [dbo].[mtRaid] ([RaidID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_mtChat_mtRaid]') AND parent_object_id = OBJECT_ID(N'[dbo].[mtChat]'))
ALTER TABLE [dbo].[mtChat]  WITH CHECK ADD  CONSTRAINT [FK_mtChat_mtRaid] FOREIGN KEY([RaidID])
REFERENCES [dbo].[mtRaid] ([RaidID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_faaFight_mtRaid]') AND parent_object_id = OBJECT_ID(N'[dbo].[faaFight]'))
ALTER TABLE [dbo].[faaFight]  WITH CHECK ADD  CONSTRAINT [FK_faaFight_mtRaid] FOREIGN KEY([RaidID])
REFERENCES [dbo].[mtRaid] ([RaidID])
